namespace com.tc.util
{

    public class DataUtil
    {
        static public int bytesValue(int myInt, byte[] data, int offset)
        {

            int hexBase = 0xff; // A byte of all ones
            data[offset + 0] = (byte)(hexBase & myInt);
            data[offset + 1] = (byte)(((hexBase << 8 & myInt)) >> 8);
            data[offset + 2] = (byte)(((hexBase << 16) & myInt) >> 16);
            data[offset + 3] = (byte)(((hexBase << 24) & myInt) >> 24);
            return 4;
        }
        static public int bytesValue(short myInt, byte[] data, int offset)
        {

            int hexBase = 0xff; // A byte of all ones
            data[offset + 0] = (byte)(hexBase & myInt);
            data[offset + 1] = (byte)(((hexBase << 8 & myInt)) >> 8);
            return 2;
        }
        static public int bytesValue(long myInt, byte[] data, int offset)
        {

            long hexBase = 0xff; // A byte of all ones
            data[offset + 0] = (byte)(hexBase & myInt);
            data[offset + 1] = (byte)(((hexBase << 8 & myInt)) >> 8);
            data[offset + 2] = (byte)(((hexBase << 16) & myInt) >> 16);
            data[offset + 3] = (byte)(((hexBase << 24) & myInt) >> 24);
            data[offset + 4] = (byte)(((hexBase << 32) & myInt) >> 32);
            data[offset + 5] = (byte)(((hexBase << 40) & myInt) >> 40);
            data[offset + 6] = (byte)(((hexBase << 48) & myInt) >> 48);
            data[offset + 7] = (byte)(((hexBase << 56) & myInt) >> 56);
            return 8;
        }
        static public byte[] bytesValue(int v)
        {
            byte[] bytes = new byte[4];
            bytesValue(v, bytes, 0);
            return bytes;
        }
        static public byte[] bytesValue(short v)
        {
            byte[] bytes = new byte[2];
            bytesValue(v, bytes, 0);
            return bytes;
        }
        static public byte[] bytesValue(long v)
        {
            byte[] bytes = new byte[8];
            bytesValue(v, bytes, 0);
            return bytes;
        }
        static public long longValue(byte[] bytes)
        {
            return longValue(bytes, 0);
        }
        static public long longValue(byte[] bytes, int offset)
        {
            long hexBase = 0xff;
            long myInt = (bytes[offset + 7] & hexBase);

            myInt = (myInt << 8) | (bytes[offset + 6] & hexBase); ;
            myInt = (myInt << 8) | (bytes[offset + 5] & hexBase);
            myInt = (myInt << 8) | (bytes[offset + 4] & hexBase);
            myInt = (myInt << 8) | (bytes[offset + 3] & hexBase);
            myInt = (myInt << 8) | (bytes[offset + 2] & hexBase);
            myInt = (myInt << 8) | (bytes[offset + 1] & hexBase);
            myInt = (myInt << 8) | (bytes[offset + 0] & hexBase);

            return myInt;
        }
        static public int intValue(byte[] bytes)
        {
            return intValue(bytes, 0);
        }
        static public int intValue(byte[] bytes, int offset)
        {
            int hexBase = 0xff;
            int myInt = (bytes[offset + 3] & hexBase);

            myInt = (myInt << 8) | (bytes[offset + 2] & hexBase);
            myInt = (myInt << 8) | (bytes[offset + 1] & hexBase);
            myInt = (myInt << 8) | (bytes[offset + 0] & hexBase);
            return myInt;
        }
        static public short shortValue(byte[] bytes)
        {

            return shortValue(bytes, 0);
        }
        static public short shortValue(byte[] bytes, int offset)
        {
            short hexBase = 0xff;
            short myInt = bytes[offset + 1];
            myInt = (short)((myInt << 8) | (bytes[offset] & hexBase));
            return myInt;
        }
        private static string hexstring = "0123456789ABCDEF";
        static public string hex(byte[] buf)
        {
            char[] hc = hexstring.ToCharArray();
            char[] sb = new char[buf.Length * 2];
            int x = 0;
            for (int j = buf.Length - 1; j >= 0; j--)
            {
                sb[x++] = hc[(buf[j] & 0xf0) >> 4];
                sb[x++] = hc[(buf[j] & 0x0f) >> 0];


            }
            return new string(sb);



        }
        static public string hex(short v)
        {

            return hex(bytesValue(v));
        }
        static public string hex(int v)
        {

            return hex(bytesValue(v));
        }
        static public string hex(byte v)
        {
            return hex(new byte[] { v});

        }
        static public void main(string[] ss)
        {
            byte[] d = bytesValue((long)-239);
            long v = longValue(d);
        }
    }
}